package com.dengqi.mymodule.activity.network.module;

import com.dengqi.mymodule.activity.network.base.BasePresenter;
import com.dengqi.mymodule.activity.network.bean.MemberBean;
import com.dengqi.mymodule.activity.network.bean.TotalNum_Lists;
import com.module.retrofit.RetrofitHelper;
import com.module.retrofit.bean.ResponseBean;
import com.module.retrofit.callback.BaseObserver;
import com.module.retrofit.callback.GlobalObserver;
import com.dengqi.mymodule.activity.network.view.IGlobal;
import com.module.retrofit.utils.CacheUtil;
import com.module.retrofit.utils.GloadUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

import static com.dengqi.mymodule.activity.network.util.ConstantUtil.ENTITY_MEMBER_LEVEL;
import static com.dengqi.mymodule.activity.network.util.ConstantUtil.ENTITY_MEMBER_TAG;


/**
 * @Author: dengqi
 * @CreateDate: 2020-10-19 19:51
 * @Description:
 */
public class MemberPresenter extends BasePresenter implements MemberContract.Presenter {

    private MemberContract.Model model;
    private int page = 1;
    private int pageSize = 20;

    public MemberPresenter(RxAppCompatActivity activity){
        super(activity);
        this.model = RetrofitHelper.getApiService(MemberContract.Model.class);
    }


    @Override
    public void getMemberList(MemberContract.MemberListView view, IGlobal iGlobal, boolean isLoadMore) {
        if(isLoadMore){
            page = page + 1;
        }else{
            page = 1;
        }
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("busId", view.getBusId());
        parmas.put("shopId", view.getShopId());
        parmas.put("ascShopId", view.getAscShopId());
        parmas.put("keyword", view.getKeyword());
        parmas.put("level", view.getLevel());
        parmas.put("source", view.getSource_());
        parmas.put("tag", view.getTag_());
        parmas.put("consumeMonth", view.getConsumeMonth());
        parmas.put("consumeCount", view.getConsumeCount());
        parmas.put("birthStart", view.getBirthStart());
        parmas.put("birthEnd", view.getBirthEnd());
        parmas.put("page", String.valueOf(page));
        parmas.put("pageSize", String.valueOf(pageSize));
        execute(model.getMemberList(parmas))
                .subscribe(new GlobalObserver<ResponseBean<TotalNum_Lists<MemberBean.ListBean>>>(activity) {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
//                        iGlobal.setGloadViewStatus(GloadUtil.STATUS_LOADING);
                    }

                    @Override
                    public void onSuccess(ResponseBean<TotalNum_Lists<MemberBean.ListBean>> result) {
                        view.onMemberList(result.getData().getList(), isLoadMore);
                        view.onTotalSize(result.getData().getTotalNum());
                        if(result.getData().getTotalNum() == 0){
                            iGlobal.setGloadViewStatus(GloadUtil.STATUS_EMPTY);
                        }else{
                            iGlobal.setGloadViewStatus(GloadUtil.STATUS_SUCCESS);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        view.onResultError(isLoadMore);
                        iGlobal.setGloadViewStatus(GloadUtil.STATUS_FAILED);
                    }
                });
    }

    @Override
    public void getMemberDetail(MemberContract.MemberDetailView view) {
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("busId", view.getBusId());
        parmas.put("busId", view.getMemberId());
        execute(model.getMemberDetail(parmas))
                .subscribe(new BaseObserver<ResponseBean<MemberBean.DetailBean>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<MemberBean.DetailBean> result) {
                        view.onMemberDetail(result.getData());
                    }

                });
    }

    @Override
    public void addMemberInfo(MemberContract.MemberInfoView view) {
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("busId", view.getBusId());
        parmas.put("shopId", view.getShopId());
        parmas.put("phone", view.getPhone());
        parmas.put("name", view.getName_());
        parmas.put("source", view.getSource_());
        parmas.put("level", view.getLevel());
        parmas.put("cardNum", view.getCardNum());
        parmas.put("birth", view.getBirth());
        parmas.put("pid", view.getPid());
        parmas.put("cid", view.getCid());
        parmas.put("did", view.getDid());
        parmas.put("address", view.getAddress());
        parmas.put("remark", view.getRemark());
        parmas.put("tag", view.getTag_());
        parmas.put("gender", view.getGender());
        execute(model.addMemberInfo(parmas))
                .subscribe(new BaseObserver<ResponseBean<Map<String, String>>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<Map<String, String>> result) {
                        view.onMemberInfo();
                    }

                });
    }

    @Override
    public void getMemberLevelList(MemberContract.MemberLevelListView view, boolean isShow) {
        List<MemberBean.LevelListBean> levelList = null;
        try {
            levelList = (List<MemberBean.LevelListBean>) CacheUtil.getFieldAsObject(ENTITY_MEMBER_LEVEL);
        }catch (Exception e){
            e.printStackTrace();
        }
      
        if(levelList != null){
            if(isShow){
                view.onMemberLevelList(levelList);
            }
            return;
        }
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("busId", view.getBusId());
        parmas.put("shopId", view.getShopId());
        execute(model.getMemberLevelList(parmas))
                .subscribe(new BaseObserver<ResponseBean<List<MemberBean.LevelListBean>>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<List<MemberBean.LevelListBean>> result) {
                        CacheUtil.saveField(ENTITY_MEMBER_LEVEL, result.getData());
                        if(isShow){
                            view.onMemberLevelList(result.getData());
                        }
                    }

                });
    }

    @Override
    public void getMemberTagList(MemberContract.MemberTagListView view, boolean isShow) {
        List<MemberBean.TagListBean> tagList = (List<MemberBean.TagListBean>) CacheUtil.getFieldAsObject(ENTITY_MEMBER_TAG);
        if(tagList != null){
            if(isShow){
                view.onMemberTagList(tagList);
            }
            return;
        }
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("busId", view.getBusId());
        parmas.put("shopId", view.getShopId());
        execute(model.getMemberTagList(parmas))
                .subscribe(new BaseObserver<ResponseBean<List<MemberBean.TagListBean>>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<List<MemberBean.TagListBean>> result) {
                        CacheUtil.saveField(ENTITY_MEMBER_TAG, result.getData());
                        if(isShow){
                            view.onMemberTagList(result.getData());
                        }
                    }

                });
    }

    @Override
    public void sendMemberSms(MemberContract.MemberSmsView view) {
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("uids", view.getUids());
        parmas.put("shopId", view.getShopId());
        parmas.put("message", view.getMessage());
        execute(model.sendMemberSms(parmas))
                .subscribe(new BaseObserver<ResponseBean<Map<String, String>>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<Map<String, String>> result) {
                        view.onMemberSms(result.getData());
                    }

                });
    }

    @Override
    public void getMemberFilter(MemberContract.MemberFilterView view) {
        execute(model.getMemberFilter())
                .subscribe(new BaseObserver<ResponseBean<MemberBean.FilterBean>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<MemberBean.FilterBean> result) {
                        view.onMemberFilter(result.getData());
                    }

                });
    }

    @Override
    public void getMemberInfoByPhone(MemberContract.MemberInfoByPhoneView view) {
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("shopId", view.getShopId());
        parmas.put("busId", view.getBusId());
        parmas.put("phone", view.getPhone());
        execute(model.getMemberInfoByPhone(parmas))
                .subscribe(new BaseObserver<ResponseBean<MemberBean.ListBean>>(activity) {

                    @Override
                    public void onSuccess(ResponseBean<MemberBean.ListBean> result) {
                        view.onMemberInfoByPhone(result.getData());
                    }

                });
    }

}
